package com.ttwl.www.app.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.ttwl.www.app.R;

/**
 * Created by Administrator on 2017/10/25 0025.
 */

public class CustomDialog extends Dialog implements View.OnClickListener {
    private TextView mTvPhoto,mTvCamera,mTvcancel;
    private String photo;
    private String camera;
    private String cancel;
    private IOnPhotoListener photoListener;
    private IOnCameraListener cameraListener;
    private IOnCancelListener cancelListener;

    public CustomDialog(@NonNull Context context) {
        super(context);
    }
    public CustomDialog(@NonNull Context context,int themeId) {
        super(context,themeId);
    }

    public CustomDialog setPhoto(String photo,IOnPhotoListener listener) {
        this.photo = photo;
        this.photoListener = listener;
        return this;
    }
    public CustomDialog setCamera(String camera,IOnCameraListener listener) {
        this.camera = camera;
        this.cameraListener = listener;
        return this;
    }
    public CustomDialog setCancel(String cancel,IOnCancelListener listener) {
        this.cancel = cancel;
        this.cancelListener = listener;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_custom_dialog);
        //设置宽度
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
        lp.y = 15; // 新位置Y坐标
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        Point size = new Point();
        d.getSize(size);
        lp.width = (int)(size.x); // 宽度
        dialogWindow.setAttributes(lp);



        mTvPhoto = (TextView) findViewById(R.id.tv_photo);
        mTvCamera = (TextView) findViewById(R.id.tv_camera);
        mTvcancel = (TextView) findViewById(R.id.tv_cancel);
        if(!TextUtils.isEmpty(photo)) {
            mTvPhoto.setText(photo);
        }
        if(!TextUtils.isEmpty(camera)) {
            mTvCamera.setText(camera);
        }
        if(!TextUtils.isEmpty(cancel)) {
            mTvcancel.setText(cancel);
        }
        mTvPhoto.setOnClickListener(this);
        mTvCamera.setOnClickListener(this);
        mTvcancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_photo:
                if(photoListener != null) {
                    photoListener.onPhoto(this);
                }
                dismiss();
                break;
            case R.id.tv_camera:
                if(cameraListener != null) {
                    cameraListener.onCamera(this);
                }
                break;
            case R.id.tv_cancel:
                if(cancelListener != null) {
                    cancelListener.onCancel(this);
                }
                dismiss();
                break;
        }
    }


    public interface IOnPhotoListener {
        void onPhoto(CustomDialog dialog);
    }
    public interface IOnCameraListener {
        void onCamera(CustomDialog dialog);
    }
    public interface IOnCancelListener {
        void onCancel(CustomDialog dialog);
    }
}
